local dianhua = fk.CreateSkill{
  name = "bd__dianhua",
}

local HG = require "packages/new_heg_diy_my/util"

Fk:loadTranslationTable{
  ["bd__dianhua"] = "点化",
  [":bd__dianhua"] = "有角色死亡后，或有武将牌回到武将牌堆时，你可以根据势力弃置一张："..
    "魏：黑色牌；蜀：红色牌；吴：装备牌；群：手牌；将其中一张对应势力武将置于武将牌上称为“化”，"..
    "然后你可以变更副将（从“化”中挑选，无次数限制）。",
  
  ["#bd__dianhua"] = "点化：弃置一张对应牌获得 %arg 武将牌",
  ["#bd__dianhua_replace"] = "点化：是否变更副将",
  ["@&bd__dianhua"] = "化"
}

local H = require "packages/ol_hegemony/util"

dianhua:addEffect(fk.GamePrepared, {
  global = true,
  can_refresh = function (self, event, target, player, data)
    return true
  end,
  on_refresh = function (self, event, target, player, data)
    player.room.returnToGeneralPile = HG.returnToGeneralPile
  end
})

dianhua:addEffect(HG.AfterReturnToGeneralPile, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_trigger = function (self, event, target, player, data)
    for _, general in ipairs(data.generals) do
      local notLord = true
      for g, l in pairs(H.lordGenerals) do
        if l == general then
          print(l, general)
          notLord = false
          break
        end
      end
      if notLord and general ~= "anjiang" and general ~= "blank_shibing" and general ~= "blank_nvshibing" then
        if (not table.contains(Fk.generals[general]:getSkillNameList(), dianhua.name)) then
          local do_cost = true
          for _, p in ipairs(player.room:getAlivePlayers(false)) do
            if table.contains(p:getTableMark("@&massacre"), general) then
              do_cost = false 
              break
            end
          end
          if do_cost and self:doCost(event, target, player, general) then
            return true
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local general = Fk.generals[data] ---@type General
    local kingdoms = {}
    if not general then return end
    table.insert(kingdoms, general.kingdom)
    if general.subkingdom then
      table.insert(kingdoms, general.subkingdom)
    end
    local discards = table.filter(player:getCardIds("he"), function (cid)
      local card = Fk:getCardById(cid)
      if (not player:prohibitDiscard(card)) then
        for _, kingdom in ipairs(kingdoms) do
          if kingdom == "qun" and table.contains(player:getCardIds("h"), cid) then
            return true
          elseif kingdom == "wei" and card.color == Card.Black then
            return true
          elseif kingdom == "shu" and card.color == Card.Red then
            return true
          elseif kingdom == "wu" and card.type == Card.TypeEquip then
            return true
          end
        end
      end
    end)
    
    local cards = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      skill_name = dianhua.name,
      pattern = ".|.|.|.|.|.|"..table.concat(discards, ","),
      include_equip = true,
      skip = true,
      cancelable = true, 
      prompt = "#bd__dianhua:::"..general.name
    })

    if #cards > 0 then
      event:setCostData(self, {
        general = general,
        card = Fk:getCardById(cards[1]),
      })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local general = cost_data.general
    local card = cost_data.card

    room:throwCard(card, dianhua.name, player)
    room:findGeneral(general.name)
    room:addTableMark(player, "@&bd__dianhua", general.name)

    if room:askToSkillInvoke(player, {skill_name = dianhua.name, prompt = "#bd__dianhua_replace"}) then
      local g = room:askToChooseGeneral(player, {
        generals = player:getTableMark("@&bd__dianhua"),
        n = 1,
        heg = false,
        no_convert = false, 
      })

      room:setEmotion(player, "./packages/ol_hegemony/image/anim/transformation")
      room:changeHero(player, g, false, true, true, false, false)
      room:removeTableMark(player, "@&bd__dianhua", g)
    end
  end
})

dianhua:addEffect(fk.Deathed, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target ~= player
  end,
  on_trigger = function (self, event, target, player, data)
    if target.general ~= "blank_shibing" and target.general ~= "blank_nvshibing" then
      if self:doCost(event, target, player, target.general) then
        return true
      end
    end
    if target.deputyGeneral ~= "blank_shibing" and target.deputyGeneral ~= "blank_nvshibing" then
      return self:doCost(event, target, player, target.deputyGeneral)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local general = Fk.generals[data] ---@type General
    local kingdoms = {}
    if not general then return end
    table.insert(kingdoms, general.kingdom)
    if general.subkingdom then
      table.insert(kingdoms, general.subkingdom)
    end
    local discards = table.filter(player:getCardIds("he"), function (cid)
      local card = Fk:getCardById(cid)
      if (not player:prohibitDiscard(card)) then
        for _, kingdom in ipairs(kingdoms) do
          if kingdom == "qun" and table.contains(player:getCardIds("h"), cid) then
            return true
          elseif kingdom == "wei" and card.color == Card.Black then
            return true
          elseif kingdom == "shu" and card.color == Card.Red then
            return true
          elseif kingdom == "wu" and card.type == Card.TypeEquip then
            return true
          end
        end
      end
    end)
    
    local cards = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      skill_name = dianhua.name,
      pattern = ".|.|.|.|.|.|"..table.concat(discards, ","),
      include_equip = true,
      skip = true,
      cancelable = true, 
      prompt = "#bd__dianhua:::"..general.name
    })

    if #cards > 0 then
      event:setCostData(self, {
        general = general,
        card = Fk:getCardById(cards[1]),
      })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local general = cost_data.general
    local card = cost_data.card

    room:throwCard(card, dianhua.name, player)
    room:findGeneral(general.name)
    room:addTableMark(player, "@&bd__dianhua", general.name)

    if room:askToSkillInvoke(player, {skill_name = dianhua.name, prompt = "#bd__dianhua_replace"}) then
      local g = room:askToChooseGeneral(player, {
        generals = player:getTableMark("@&bd__dianhua"),
        n = 1,
        heg = false,
        no_convert = false, 
      })

      room:setEmotion(player, "./packages/ol_hegemony/image/anim/transformation")
      room:changeHero(player, g, false, true, true, false, false)
      room:removeTableMark(player, "@&bd__dianhua", g)
    end
  end
})

dianhua:addEffect(fk.AfterSkillEffect, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self, true, true) and data.skill.skeleton.name == "shilus"
  end,
  on_refresh = function (self, event, target, player, data)
    local shilus = data.skill
    local generals = player:getTableMark("@&bd__dianhua")
    local repeat_general = data.who:getTableMark("@&massacre")
    for _, general in ipairs(generals) do
      if table.contains(repeat_general, general) then
        player.room:removeTableMark(data.who, "@&massacre", general)
      end
    end
  end
})

dianhua:addLoseEffect(function (self, player, is_death)
  local generals = player:getTableMark("@&bd__dianhua")
  player.room:returnToGeneralPile(generals)
  player.room:setPlayerMark(player, "@&bd__dianhua", 0)
end)

return dianhua